package com.tdp.excise;

import java.util.List;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class MenuHandle {
	public Document getDocument(String fileName) throws Exception{
		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(this.getClass().getResourceAsStream("/"+fileName));
		return document;
	}
	
	public Menu findFunctionLevel(String Name) throws Exception{
		Menu menu = new Menu();
		Document document = this.getDocument("menu.xml");
		Element root = document.getRootElement();
		List<Element> function_nodes = root.elements("function");
		for (Element function_node : function_nodes) {
			String attribureValue = function_node.attributeValue("level");
				if(attribureValue.contains(Name)||attribureValue.equalsIgnoreCase("all"))
					{
					Function function = new Function();
					function.setLeve(function_node.attributeValue("level"));
					function.setName(function_node.attributeValue("name"));
					List<Element> nodes = function_node.elements();
					for (Element node : nodes) {
						Node nn = new Node();
						nn.setLabel(node.attributeValue("label")+":"+node.getText());		
						function.getNodes().add(nn);
						}
					
					menu.getFunctions().add(function);
					}
			
			}
		return menu;
	}
	
	public static void main(String[] args) throws Exception {
		Menu menu = new MenuHandle().findFunctionLevel("scott");
		Set<Function> functions = menu.getFunctions();
		for (Function function : functions) {
			System.out.print(function.getName()+" ");
			System.out.println(function.getLeve()+" ");
			Set<Node> nodes = function.getNodes();
			for (Node node : nodes) {
				System.out.println(node.getLabel()+" ");
			}
			System.out.println();
		}
	}
}
